/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kitaplaner.application;

import java.sql.Connection;
import java.sql.Time;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import kitaplaner.database.Database;
import kitaplaner.database.Gruppe;
import kitaplaner.database.Kita;
import kitaplaner.database.Mitarbeiter;

/**
 *
 * @author kristina
 */
public class GruppenApp
{
    private Kita kita;
    private Database db = new Database();
    private Connection conn;
    private MitarbeiterApp maApp;

    public GruppenApp(Kita kita, Connection conn)
    {
        this.conn = conn;
        maApp = new MitarbeiterApp(kita, conn);
        this.kita = kita;
    }

    public Boolean changeGrpPrepTime (Gruppe grp)
    {
        Boolean success = false;
        int time = 0;
        ArrayList <Mitarbeiter> ma = maApp.getMitarbeiterByGroup(grp.getId());

        for (int i = 0; i < ma.size(); i++)
        {
            time += ma.get(i).getStdVorbereitung();
        }

        grp.setVorbZeit(time);
        conn = db.checkConnection(conn);
        success = db.changeGrp(conn, grp);
        conn = db.checkConnection(conn);
        kita.setGrp(db.getGruppen(conn));
        
        return success;
    }

    public Object [] createCompoboxText()
    {
        Object [] comboItems;
        
        ArrayList <Gruppe> grp = kita.getGrp();
        ArrayList <String> grpNames = new ArrayList<String>();

        for (int i = 0; i < grp.size(); i++)
        {
            grpNames.add(grp.get(i).getName());
        }
        comboItems = grpNames.toArray();
        
        return comboItems;
    }

    public DefaultTableModel createGrpOverview()
    {
        String[] columnNames = {"ID","Name", "Startzeit", "Endzeit", "Vorbereitungszeit",
                                "Leitung Arbeitszeit"};

        ArrayList <Gruppe> grp = kita.getGrp();
        DefaultTableModel model = new DefaultTableModel();
        JTable table = new JTable(model);

        //insert Columns
        for (int i = 0 ; i < columnNames.length; i++)
        {
            model.addColumn(columnNames[i]);
        }

        for (int i = 0; i < grp.size(); i++)
        {
            Object []obj = {grp.get(i).getId(), grp.get(i).getName(),
                            grp.get(i).getStart(), grp.get(i).getEnd(),
                            grp.get(i).getVorbZeit(), grp.get(i).getLeitZeit()};
            model.insertRow (model.getRowCount(), obj);
        }
        return model;
    }

    public Gruppe getGruppeByName (String grpName)
    {
        Gruppe grp = null;

        ArrayList<Gruppe> listGrp = kita.getGrp();

        for (int i = 0; i < listGrp.size(); i++)
        {
            if (listGrp.get(i).getName().equals(grpName))
            {
                grp = listGrp.get(i);
            }
        }

        return grp;
    }

    public Boolean insertIntoGroup(String name, Time start, Time end, double prepTime, double leadTime)
    {
        Boolean success = false;
        Gruppe grp = new Gruppe ();

        grp.setName(name);
        grp.setEnd(end);
        grp.setStart(start);
        grp.setLeitZeit(leadTime);
        grp.setVorbZeit(prepTime);

        conn = db.checkConnection(conn);
        success = db.insertGruppe(conn, grp);
        conn = db.checkConnection(conn);
        kita.setGrp(db.getGruppen(conn));

        return success;
    }

    public Boolean changeGrpEntry (int id, String name, Time start, Time end, double prepTime, double leadTime)
    {
        Boolean insert = false;

        Gruppe grp = new Gruppe ();
        grp.setEnd(end);
        grp.setStart(start);
        grp.setName(name);
        grp.setId(id);
        grp.setVorbZeit(prepTime);
        grp.setLeitZeit(leadTime);

        ArrayList <Gruppe> alGrp = kita.getGrp();

        for (int i = 0; i < alGrp.size(); i++)
        {
            if (alGrp.get(i).getId() == id)
            {
                alGrp.get(i).setEnd(end);
                alGrp.get(i).setStart(start);
                alGrp.get(i).setLeitZeit(leadTime);
                alGrp.get(i).setVorbZeit(prepTime);
                alGrp.get(i).setName(name);
                insert = true;
            }
        }
        kita.setGrp(alGrp);

        conn = db.checkConnection(conn);
        insert = db.changeGrp(conn, grp);

        return insert;
    }

    public int getGrpIdByName (String name)
    {
        ArrayList <Gruppe> alGrp = kita.getGrp();
        int grpId = -1;

        for (int i = 0; i < alGrp.size(); i++)
        {
            if (alGrp.get(i).getName().equals(name))
            {
                grpId = alGrp.get(i).getId();
            }
        }

        return grpId;
    }

    public String getGrpNameById (int id)
    {
        String grpName = "";
        ArrayList <Gruppe> alGrp = kita.getGrp();

        for (int i = 0; i < alGrp.size(); i++)
        {
            if (alGrp.get(i).getId() == id)
            {
                grpName = alGrp.get(i).getName();
            }
        }

        return grpName;
    }

    public Boolean deleteGrpEntry (int id)
    {
        Boolean success = false;
        ArrayList <Gruppe> aG = kita.getGrp();

        for (int i = 0; i < aG.size(); i ++)
        {
            if (aG.get(i).getId() == id)
            {
                aG.remove(i);
            }
        }

        conn = db.checkConnection(conn);
        success = db.deleteGrp(conn, id);

        kita.setGrp(aG);

        return success;
    }
}